package com.example.sikong.contoller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sikong.entity.SkDeviceConfig;
import com.example.sikong.service.ISkDeviceConfigService;
import com.example.sikong.service.RedisProvider;
import com.example.sikong.service.SiKongApiClientService;
import com.example.websocket.util.response.WebResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author best-man-ever
 * @version 1.0
 * @date 2022/3/28 20:38
 */
@Slf4j
@RestController
@RequestMapping("/public")
public class SikongLiveController {

    @Autowired
    private SiKongApiClientService siKongApiClientService;
    @Autowired
    ISkDeviceConfigService skDeviceConfigService;
    @Autowired
    private RedisProvider redisProvider;
    @Value("${si-kong.heartbeatTime}")
    int heartbeatTime;

    /**
     * 获取所有无人机设备信息
     */
    @GetMapping(value = {"getAllDeviceList","getAllDevicelist"})
    public WebResponse getAllDeviceList(@RequestParam String hdbm) {
        SkDeviceConfig skDeviceConfig = new SkDeviceConfig();
        skDeviceConfig.setHdbm(hdbm);
        List<SkDeviceConfig> list = skDeviceConfigService.list(new QueryWrapper<>(skDeviceConfig));
        return WebResponse.ok(list);
    }

    /**
     * 根据SN无人机编号开启直播
     */
    @PostMapping("/sk/start/live")
    public WebResponse startLive(@Validated @RequestBody SkStarLiveModel model) {
        Map<String, Object> map = siKongApiClientService.multipleStartHangarDeviceLive(model.getSn());
        return WebResponse.ok(map);
    }

    /**
     * 接口-根据SN无人机编号关闭直播
     */
    @GetMapping("/sk/stop/live")
    public WebResponse stopLive(@RequestParam String sn, @RequestParam String converterId) {
        log.info("接口-根据SN无人机编号关闭直播 sn:{},converterId:{}", sn, converterId);
        boolean b = siKongApiClientService.stopLiveByInLiveKey(sn, converterId);
        return WebResponse.ok(b);
    }

    /**
     * 观看直播时候开启心跳
     */
    @RequestMapping(value = "heartbeat", method = {RequestMethod.POST, RequestMethod.GET})
    public WebResponse heartbeat(@RequestParam String sn, @RequestParam String converterId) {
        log.info("观看直播时候开启心跳 sn:{},converterId:{}", sn, converterId);
        //设置到数据库或者redis，后面如果没有心跳就删除该直播
        redisProvider.set(converterId, converterId, heartbeatTime);
        return WebResponse.ok();
    }

    /**
     * 司空2设置的回调
     */
//    @RequestMapping(value = "webhook/sk2", method = {RequestMethod.POST, RequestMethod.GET})
//    public Map<String, Object> webhookSk2Post(@RequestParam Map<String, Object> parmas, @RequestBody(required = false) Map<String, Object> body) {
//        Map<String, Object> result = new HashMap<>();
//        System.out.println(JSON.toJSONString(parmas));
//        System.out.println(JSON.toJSONString(body));
//        result.put("code", "200");
//        return result;
//    }


}
